package jiang.app.service;

import javax.annotation.Resource;

import jiang.app.dao.ICandidateDao;
import jiang.app.dao.IConnectDao;
import jiang.app.dao.IExamDao;
import jiang.app.dao.IInterviewDao;
import jiang.app.dao.IReviewDao;
import jiang.app.entity.Candidate;
import jiang.app.entity.Connection;
import jiang.app.entity.Exam;
import jiang.app.entity.Interview;
import jiang.app.entity.Review;

import org.springframework.stereotype.Component;

@Component
public class StepService {
	@Resource
	IConnectDao connectDao;
	@Resource
	IReviewDao reviewDao;
	@Resource
	IInterviewDao interviewDao;
	@Resource
	IExamDao examDao;
	@Resource
	ICandidateDao candidateDao;


	public Review initReviewCandidate(long candidateId) {
		Review r = reviewDao.selectReviewByCandidateId(candidateId);
		// 如果没有关联，就关联
		if (r == null) {
			r = new Review();
			r.setCandidateId(candidateId);
			reviewDao.insertReview(r);
		}
		return r;
	}

	public void updateReviewCandidate(Review r) {
		reviewDao.updateReview(r);
	}

	public Candidate findCandidateById(long id) {
		return candidateDao.selectCandidateById(id);
	}

	public void updateCandidateState_Review(long candidateId, int reviewState) {
		Candidate c = findCandidateById(candidateId);
		if (reviewState == 1) {
			//初步审核通过
			c.setState(c.getState() + 1);
		}
		else {
			//初步审核不通过
			c.setState(5);
		}
		candidateDao.updateCandidateStateByCandidateId(c);
	}

	//Connection
	public Connection initConnectionCandidate(long candidateId) {
		Connection c = connectDao.selectConnectionByCandidateId(candidateId);
		// 如果没有关联，就关联
		if (c == null) {
			c = new Connection();
			c.setCandidateId(candidateId);
			connectDao.insertConnection(c);
		}
		return c;
	}
	
	public void updateConnectionCandidate(Connection c) {
		connectDao.updateConnection(c);
	}
	
	public void updateCandidateState_Connection(long candidateId, int connectionState) {
		Candidate c = findCandidateById(candidateId);
		if (connectionState == 2) {
			//联系
			c.setState(c.getState() + 1);
		}
		else {
			//不联系，终止
			c.setState(5);
		}
		candidateDao.updateCandidateStateByCandidateId(c);
	}
	
	//Exam
	public Exam initExamCandidate(long candidateId) {
		Exam e = examDao.selectExamByCandidateId(candidateId);
		// 如果没有关联，就关联
		if (e == null) {
			e = new Exam();
			e.setCandidateId(candidateId);
			examDao.insertExam(e);
		}
		return e;
	}
	
	public void updateExamCandidate(Exam e) {
		examDao.updateExam(e);
	}
	
	public void updateCandidateState_Exam(long candidateId, int examState) {
		Candidate c = findCandidateById(candidateId);
		if (examState == 2) {
			//通过Exam
			c.setState(c.getState() + 1);
		}
		else {
			//不联系，终止
			c.setState(5);
		}
		candidateDao.updateCandidateStateByCandidateId(c);
	}
	
	//interview
	public Interview initInterviewCandidate(long candidateId) {
		Interview i = interviewDao.selectInterviewByCandidateId(candidateId);
		// 如果没有关联，就关联
		if (i == null) {
			i = new Interview();
			i.setCandidateId(candidateId);
			interviewDao.insertInterview(i);
		}
		return i;
	}
	
	public void updateInterviewCandidate(Interview i) {
		interviewDao.updateInterview(i);
	}
	
	public void updateCandidateState_Interview(long candidateId, int interviewState) {
		Candidate c = findCandidateById(candidateId);
		if (interviewState == 2) {
			//审核全部通过，被录取
			c.setState(6);
		}
		else {
			//不联系，终止
			c.setState(5);
		}
		candidateDao.updateCandidateStateByCandidateId(c);
	}
}
